import axios from 'axios'
import { message } from 'ant-design-vue'

const service = axios.create({
  baseURL: import.meta.env.VITE_BASE_API,
 
  timeout: 500000, // request timeout
  
})

service.interceptors.request.use(
  (config) => {

    let url = config.url
    if (config.method === 'get') {
      url.indexOf('?') === -1 ? config.url = url + '?timeStamp=' + (new Date().getTime()) : config.url = url + '&timeStamp=' + (new Date().getTime())
    }


    const token = sessionStorage.getItem('lwgz-sessionId')
    token ? config.headers.Authorization = token : null
    // config.headers.Authorization='304196fd-38c2-4747-9691-09563539b9bf'

    return config
  },
  (error) => {
    return Promise.reject(error)
  }
)

service.interceptors.response.use(
  (response) => {
    const res = response.data
  
    if (res.code != 200) {
      if (res.code == 403) {
        message.destroy()
        message.warning('会话失效，请重新登录。')

      } else {
        message.destroy()
        message.error(res.message)
      }

      return Promise.reject('error')
    } else {
      return response
    }
  },
  (error) => {
    
    console.log(error.response)

    return Promise.reject(error)
  }
)

export default service
